﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SVC.client;
namespace UI.Forms
{
    public partial class ComboClass_U : DevExpress.XtraEditors.XtraForm
    {
        public delegate void DoAction();//如果有参数的话，定义参数 

        public DoAction FatherRefresh;
        private int count=0;
        string classcode = "";
        SVC.client.Combo_Class c;
        public ComboClass_U(string _classcode)
        {
            InitializeComponent();
            classcode = _classcode;
        }




        private void btn_new_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!dxValidationProvider1.Validate()) return;
            SVC.client.Combo_Class cc = new SVC.client.Combo_Class();
            cc.ClassCode = te_ClassCode.Text.Trim();
            cc.ClassName = te_name.Text.Trim();
            if (!string.IsNullOrEmpty(popupContainerEdit1.Text))
            { 
                cc.ParentCode=popupContainerControl1.Text.Split(',')[0];
            }


            string result = my.bw.Combo_Class_Update(cc, my.ACID);
            if (string.IsNullOrWhiteSpace(result))
            {
                count++;
                this.Close();

            }
            else
            {
                Comm.ShowErorr(result);
            }
        }

        private void btn_newclose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!dxValidationProvider1.Validate()) return;
            SVC.client.Combo_Class cc = new SVC.client.Combo_Class();
            cc.ClassCode = te_ClassCode.Text.Trim();
            cc.ClassName = te_name.Text.Trim();
            string result = my.bw.Combo_Class_Add(cc, my.ACID);
            if (string.IsNullOrWhiteSpace(result))
            {
                count++;
                this.Close();
            }
            else
            {
                Comm.ShowErorr(result);
            }
        }


        private void ComboClass_N_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (count > 0)
            {
                FatherRefresh();
            }
        }

        private void ComboClass_N_Load(object sender, EventArgs e)
        {
            bind();
            c = my.bw.Combo_ClassByClassCode_Get(classcode, my.ACID);
            te_ClassCode.Text = c.ClassCode;
            te_name.Text = c.ClassName;
            if (!string.IsNullOrEmpty(c.ParentCode))
	        {
		        SVC.client.Combo_Class cp=my.bw.Combo_ClassByClassCode_Get(c.ParentCode,my.ACID);
                popupContainerEdit1.Text = string.Format("{0},{1}",cp.ClassCode,cp.ClassName);
            }
            

        }

        private  void bind()
        {
            treeList_comboclass.DataSource = my.bw.Combo_Class_Get("ClassCode", my.ACID);
            treeList_comboclass.ExpandAll();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            SVC.client.Combo_Class PGC = treeList_comboclass.GetDataRecordByNode(treeList_comboclass.FocusedNode) as SVC.client.Combo_Class;
            if (PGC == null) return;
            popupContainerEdit1.EditValue = string.Format("{0},{1}", PGC.ClassCode, PGC.ClassName);
            popupContainerEdit1.ClosePopup();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            popupContainerEdit1.Text = "";
            popupContainerEdit1.ClosePopup();
        }

    }
}